package com.hm.hall.commons.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期,时间的工具
 * <p>
 * <p>
 * 一天的time : (1000 * 60 * 60 * 24) 获得24小时前: new Date( System.currentTimeMillis()
 * - (1000 * 60 * 60 * 24) )
 */
public class DateUtils {
    public static final long A_DAY_TIME = 1000 * 60 * 60 * 24; // 一天的time ,用于快速计算昨天呀,明天什么的
    public static final long A_HOUR_TIME = 1000 * 60 * 60; // 一小时的time ,用于快速计算 时间差
    public static final long A_DAY_SEC = 60 * 60 * 24; // 一天的秒数 ,用于快速计算昨天呀,明天什么的
    public static final int A_WEEK_SEC = 7 * 60 * 60 * 24; // 一周的秒数 ,用于快速计算昨天呀,明天什么的
    public static final long A_HOUR_SEC = 60 * 60; // 一小时的秒数 ,用于快速计算 时间差
    public static final long A_MINUTE_SEC = 60; // 一小时的秒数 ,用于快速计算 时间差

    /**
     * parse String format "yyyy-MM-dd HH:mm:ss" to Date
     */
    public static Date parseDate(String strDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return sdf.parse(strDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * parse String format by format
     */
    public static Date parseDate(String strDate, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            return sdf.parse(strDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * format Date to String, format
     */
    public static String formatDate(Date date, String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        return formatter.format(date);
    }

    /**
     * format Date to String, "yyyy-MM-dd HH:mm:ss"
     */
    public static String formatDate(Date date) {
        return formatDate(date, "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * format current Date "yyyy-MM-dd HH:mm:ss"
     */
    public static String formatDate() {
        return formatDate(new Date());
    }

    public static boolean isTimeout(int expire, int delay, long lastUpdateTime) {
        return (System.currentTimeMillis() - lastUpdateTime) > (expire + delay) * 1000 ? true : false;
    }

    /**
     * 今天零点零分零秒
     */
    public static Date currentDate() {
        return getZeroDate(new Date());
    }

    /**
     * 获得 零点零分零秒 的日期
     */
    public static Date getZeroDate(Date time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.set(Calendar.HOUR_OF_DAY, 0);// 将小时至0
        calendar.set(Calendar.MINUTE, 0); // 将分钟至0
        calendar.set(Calendar.SECOND, 0); // 将秒至0
        calendar.set(Calendar.MILLISECOND, 0); // 将毫秒至0
        return calendar.getTime();
    }

    /**
     * 用年龄获得出生日期
     */
    public static Date getBirthDateByAge(int age) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.YEAR, -age);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 判断是否为同一天
     */
    public static boolean isSameDay(Date date1, Date date2) {

        if (date1 == null || date2 == null) {
            return false;
        }

        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);

        boolean isSameYear = cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR);
        boolean isSameMonth = isSameYear && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH);
        boolean isSameDate = isSameMonth && cal1.get(Calendar.DAY_OF_MONTH) == cal2.get(Calendar.DAY_OF_MONTH);

        return isSameDate;
    }

    /**
     * 获得今天的剩余时间 (毫秒)
     *
     * @return 今天剩余时间 (毫秒)
     */
    public static long getTodayResidueTime() {
        Calendar c = Calendar.getInstance();
        long now = c.getTimeInMillis();
        c.add(Calendar.DAY_OF_MONTH, 1);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTimeInMillis() - now;
    }

    /**
     * 获得今天的剩余时间 (秒)
     *
     * @return 今天剩余时间 (秒)
     */
    public static int getTodayResidueSec() {
        return (int) (getTodayResidueTime() / 1000);
    }

    /**
     * 获得10位时间戳
     */
    public static long getTime10() {
        return System.currentTimeMillis() / 1000;
    }

    /**
     * 根据用户生日计算年龄 , 计算的是周岁不是虚岁
     */
    public static int getAgeByBirthday(Date birthday) {
        if (birthday == null) {
            return 0;
        }
        Calendar cal = Calendar.getInstance();
        if (cal.before(birthday)) {
            throw new IllegalArgumentException("The birthDay is before Now.It's unbelievable!");
        }

        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH) + 1;
        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);

        cal.setTime(birthday);
        int yearBirth = cal.get(Calendar.YEAR);
        int monthBirth = cal.get(Calendar.MONTH) + 1;
        int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);

        int age = yearNow - yearBirth;

        if (monthNow <= monthBirth) {
            if (monthNow == monthBirth) {
                // monthNow==monthBirth
                if (dayOfMonthNow < dayOfMonthBirth) {
                    age--;
                }
            } else {
                // monthNow>monthBirth
                age--;
            }
        }
        return age;
    }

    /**
     * 根据时分来获取当天的具体时间
     *
     * @return
     */
    public static Date convertTodayTime(Date oldTime) {
        Date now = new Date();
        Calendar todayCal = Calendar.getInstance();
        todayCal.setTime(now);
        Calendar oldCal = Calendar.getInstance();
        oldCal.setTime(oldTime);
        todayCal.setTime(now);
        todayCal.set(Calendar.HOUR_OF_DAY, oldCal.get(Calendar.HOUR_OF_DAY));
        todayCal.set(Calendar.MINUTE, oldCal.get(Calendar.MINUTE));
        todayCal.set(Calendar.SECOND, 0);
        return new Date(todayCal.getTime().getTime());
    }

    /**
     * 取上n周 周一的日期 n为推迟的周数，1本周，-1向前推迟一周，2下周，依次类推
     */
    public static Date getOffsetWeekDate(int n) {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, n * 7);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        return cal.getTime();
    }

    public static String formatTime(long ms) {

        int ss = 1000;
        int mi = ss * 60;
        int hh = mi * 60;
        int dd = hh * 24;

        long day = ms / dd;
        long hour = (ms - day * dd) / hh;
        long minute = (ms - day * dd - hour * hh) / mi;
        long second = (ms - day * dd - hour * hh - minute * mi) / ss;
        long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;

        String strDay = day < 10 ? "0" + day : "" + day; // 天
        String strHour = hour < 10 ? "0" + hour : "" + hour;// 小时
        String strMinute = minute < 10 ? "0" + minute : "" + minute;// 分钟
        String strSecond = second < 10 ? "0" + second : "" + second;// 秒
        String strMilliSecond = milliSecond < 10 ? "0" + milliSecond : "" + milliSecond;// 毫秒
        strMilliSecond = milliSecond < 100 ? "0" + strMilliSecond : "" + strMilliSecond;

        return strHour + ":" + strMinute + ":" + strSecond;
    }

    public static long getHourAndMinute(Date date) {

        Calendar todayCal = Calendar.getInstance();
        todayCal.setTime(date);

        long hour = todayCal.get(Calendar.HOUR_OF_DAY);
        long minute = todayCal.get(Calendar.MINUTE);
        return hour * 60 + minute;
    }

    /**
     * 计算相差的天数
     *
     * @param data
     * @param regTime
     * @return
     */
    public static int calculateDay(Date date, Date regTime) {
        Calendar aCalendar = Calendar.getInstance();
        aCalendar.setTime(date);
        int day1 = aCalendar.get(Calendar.DAY_OF_YEAR);
        aCalendar.setTime(regTime);
        int day2 = aCalendar.get(Calendar.DAY_OF_YEAR);
        return day1 - day2;
    }

    public static void main(String[] args) {
        // String str = formatDate(new Date(System.currentTimeMillis() - A_DAY_TIME));

        // System.out.println(str);
        //
        // long current = System.currentTimeMillis();//当前时间毫秒数
        // long zero = current / (1000 * 3600 * 24) * (1000 * 3600 * 24) -
        // TimeZone.getDefault().getRawOffset();//今天零点零分零秒的毫秒数
        // long twelve = zero + 24 * 60 * 60 * 1000 - 1;//今天23点59分59秒的毫秒数
        // long yesterday = System.currentTimeMillis() - 24 * 60 * 60 *
        // 1000;//昨天的这一时间的毫秒数
        // System.out.println(new Timestamp(current));//当前时间
        // System.out.println(new Timestamp(yesterday));//昨天这一时间点
        // System.out.println(new Timestamp(zero));//今天零点零分零秒
        // System.out.println(new Timestamp(twelve));//今天23点59分59秒
        System.out.printf(formatDate(getBirthDateByAge(24)));

    }
}
